package com.ruoyi.common.utils.tool;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Utils {

    // 32位MD5加密
    public static String encode32(String content) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(content.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("NoSuchAlgorithmException", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("UnsupportedEncodingException", e);
        }
        // 对生成的16字节数组进行补零操作
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    // 生成签名
    public static String generateSign(String appId, String appSecret, String timestamp) {
        return encode32(appId + encode32(appSecret) + timestamp);
    }

    // 生成人脸ID
    public static String generateFaceId() {
        return encode32(System.currentTimeMillis() + TextUtils.getRandom(10));
    }

}
